package com.ruoyi.web.task;


import com.alibaba.excel.util.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.student.domain.StudentBaseInfo;
import com.ruoyi.student.domain.XsXsjbsj;
import com.ruoyi.student.service.IStudentBaseInfoService;
import com.ruoyi.student.service.IStudentEducationalLocalService;
import com.ruoyi.student.service.IStudentEducationalRemoteService;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.List;


/**
 * 同步本地
 */
@Component("synStudentDataJob")
public class SynStudentDataJobImp {

    /**
     * 学生基础信息模块
     */
    @Resource
    public IStudentBaseInfoService studentBaseInfoService;


    /**
     * 教务远程查询模块
     */
    @Resource
    private IStudentEducationalRemoteService studentEducationalRemoteService;

    /**
     * 教务本地查询模块
     */
    @Resource
    private IStudentEducationalLocalService studentEducationalLocalService;

    /**
     * 参数模块
     */
    @Resource
    public ISysConfigService iSysConfigService;

    public void execute() throws IOException {
        FileWriter fw;//日志文件写句柄
        String logPath = iSysConfigService.selectConfigByKey("sys.log.file.path");//日志导出路径
        String fileName = DateUtils.format(new Date(), DateUtils.DATE_FORMAT_14) + "-" + UUID.fastUUID().toString().replace("-", "") + ".txt";//随机生成uuid文件名
        String fullPath = logPath + fileName;
        File file = new File(fullPath);
        file.createNewFile();//新建日志文件
        fw = new FileWriter(fullPath);
        Date operateDate = com.ruoyi.common.utils.DateUtils.getNowDate();
        try {
            fw.write("操作开始：" + DateUtils.format(new Date(), DateUtils.DATE_FORMAT_19) + "\r\n");
            fw.write("准备清空表数据：" + DateUtils.format(new Date(), DateUtils.DATE_FORMAT_19) + "\r\n");
            //1.清空本地所有表数据
            studentEducationalLocalService.deleteAll();
            //2.查询所有远程表
            List<XsXsjbsj> remoteList = studentEducationalRemoteService.selectStudyStudentList();
            //3.插入到本地表
            for (XsXsjbsj item : remoteList) {
                studentEducationalLocalService.insert(item);
            }
            //4.清洗数据
            studentEducationalLocalService.cleanJxData();
            fw.write("远程数据导入本地数据结束：" + DateUtils.format(new Date(), DateUtils.DATE_FORMAT_19) + "\r\n");
            fw.write("同步本地数据：" + DateUtils.format(new Date(), DateUtils.DATE_FORMAT_19) + "\r\n");
            //5.更新在学状态
            List<XsXsjbsj> changeList = studentEducationalLocalService.selectChangeList();
            fw.write("筛选出学籍变动学生" + changeList.size() + "条：" + DateUtils.format(new Date(), DateUtils.DATE_FORMAT_19) + "\r\n");
            for (XsXsjbsj item : changeList) {
                StudentBaseInfo studentBaseInfo = new StudentBaseInfo();
                studentBaseInfo.setCode(item.getXh());
                studentBaseInfo.setStudyState(item.getXjzt());
                studentBaseInfoService.taskUpdateStudentInfo(studentBaseInfo);
            }
            fw.write("处理学籍变动学生结束" + DateUtils.format(new Date(), DateUtils.DATE_FORMAT_19) + "\r\n");
            //6.更新姓名
            fw.write("开始更新改名学生" + DateUtils.format(new Date(), DateUtils.DATE_FORMAT_19) + "\r\n");
            studentEducationalLocalService.synStudentName();
            fw.write("更新学生姓名结束" + DateUtils.format(new Date(), DateUtils.DATE_FORMAT_19) + "\r\n");
        } catch (Exception e) {
            fw.write("**********************************************************************");
            fw.write("数据同步出错");
            fw.write(e.getMessage());
            fw.write("——————————————————————————————————————————————————————————————————————");
            fw.write(e.getStackTrace().toString());
            fw.write("**********************************************************************");
        } finally {
            try {
                fw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }


}
